package com.zdp.seckill.controller;

import cn.hutool.core.lang.UUID;
import com.zdp.seckill.common.domain.ResultInfo;
import com.zdp.seckill.common.utils.AssertUtil;
import com.zdp.seckill.common.utils.ResultInfoUtil;
import com.zdp.seckill.pojo.vo.UserModel;
import com.zdp.seckill.pojo.vo.UserVO;
import com.zdp.seckill.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

/**
 * @author sesshomaru
 * @date 2021/6/17
 */
@Slf4j
@RestController
@RequestMapping("user")
public class UserController extends BaseController{

    @Resource
    private UserService userService;

    @Resource
    private HttpServletRequest httpServletRequest;

    //用户登陆接口
    @PostMapping(value = "/login", consumes={CONTENT_TYPE_FORMED})
    @ResponseBody
    public ResultInfo login(@RequestParam(name="telphone")String telphone,
                            @RequestParam(name="password")String password) {
        UserModel userModel = userService.validateLogin(telphone, EncodeByMd5(password));

        this.httpServletRequest.getSession().setAttribute("IS_LOGIN",true);
        this.httpServletRequest.getSession().setAttribute("LOGIN_USER",userModel);

        return ResultInfoUtil.buildSuccess(getHttpServletRequest().getRequestURI(), "登录成功");
    }

    //用户注册接口
    @PostMapping(value = "/register",consumes={CONTENT_TYPE_FORMED})
    public ResultInfo register(@RequestParam(name="telphone")String telphone,
                                     @RequestParam(name="otpCode")String otpCode,
                                     @RequestParam(name="name")String name,
                                     @RequestParam(name="gender")Integer gender,
                                     @RequestParam(name="age")Integer age,
                                     @RequestParam(name="password")String password) {
        //验证手机号和对应的otpcode相符合
        HttpSession session = httpServletRequest.getSession();
        // 这里为了过验证，实际从Redis中获取验证码
        String sessionAttribute = (String) session.getAttribute(telphone);
        // String inSessionOtpCode = sessionAttribute == null ? otpCode : sessionAttribute;
        AssertUtil.strEquals(sessionAttribute, otpCode, "验证码校验失败");
        //用户的注册流程
        UserModel userModel = new UserModel();
        userModel.setName(name);
        userModel.setGender(gender);
        userModel.setAge(age);
        userModel.setTelphone(telphone);
        userModel.setRegisterMode("byphone");
        userModel.setEncrptPassword(EncodeByMd5(password));
        userService.register(userModel);
        return ResultInfoUtil.buildSuccess(httpServletRequest.getRequestURI(), "注册成功");
    }

    @PostMapping(value = "/getotp", consumes = {CONTENT_TYPE_FORMED})
    public ResultInfo getOtp(@RequestParam("telphone")String telphone) {
        // 按照一定规则生成OTP验证码
        Random random = new Random();
        int randomInt = random.nextInt(99999);
        randomInt += 10000;
        String optCode = String.valueOf(randomInt);

        // 将otp验证码同对应的用户手机号关联, 推荐使用redis做
        HttpSession session = httpServletRequest.getSession();
        // 关联一下
        session.setAttribute(telphone, optCode);

        //TODO 将otp验证码发送给用户, 忽略

        log.info("telphone:{}, optCode:{}", telphone, optCode);

        return ResultInfoUtil.buildSuccess(httpServletRequest.getRequestURI());
    }


    @RequestMapping("/get")
    public ResultInfo getUser(@RequestParam("id")Integer id) {
        UserModel userModel = userService.getUserById(id);
        return ResultInfoUtil.buildSuccess(httpServletRequest.getRequestURI(), convertFromModel(userModel));
    }

    private UserVO convertFromModel(UserModel userModel) {
        AssertUtil.isNotNull(userModel, "userModel为空");
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(userModel, userVO);
        return userVO;
    }
}
